/**
 * @author abid.hussain
 */
exports.GAConfigurations = Core.Class.singleton({

	classname : 'GAConfigurations',
	
	intialize : function()
	{
			this._udid = null;
			this._responseObject = null;
			this._gameCode=null;
			this._deviceType = null;
			this._osVersion = null;
			this._deviceName = null;
			this._gameVersion = null;
			this._gameName = null;
			this._gameFamily = null; 
			this._url = null;
			this._gameStateDBPath = null;
			this._gameFamilyId= null;
			this._gameFlavor=null;
			this._resourcesBasePath = null;
			this._userId = null;
			this._recordId = null;
			this._dbFileNames = null;
			this._level = 1;
			this._experience = 0;
			this._callback = null;
	},

	setGapiConfiguration : function(gameCode,gameVersion,gameName, gameFamily, url, gamePath, resourcesBasePath,fileNames, callback)
	{
		var errorMsg = null;
		if(typeof(gameCode) == 'undefined' || gameCode == "") {
			errorMsg = "\n - Game code cannot be empty";
		}
		if(typeof(gameName) == 'undefined' || gameName == "") {
			errorMsg = "\n - Game name cannot be empty";
		}
		if(typeof(gameFamily) == 'undefined' || gameFamily == "") {
			errorMsg = "\n - Game Family cannot be empty";
		}
		if(typeof(gameVersion) == 'undefined' || gameVersion < 0.0) {
			errorMsg = "\n - Game Version cannot less than 0.0";
		}
		if(typeof(url) == 'undefined' || url == "") {
			errorMsg = "\n - Game Connect URL can not be empty";
		}
		if(typeof(gamePath) == 'undefined' || gamePath == ""){
			errorMsg = "\n - GameState DB path can not be empty";
		}
		if(typeof(resourcesBasePath) == 'undefined' || resourcesBasePath ==""){
			errorMsg = "\n - resourcesBasePath can not be empty";
		}
		if(typeof(fileNames) == 'undefined' || fileNames == ""){
			
			errorMsg = "\n - fileNames parameter can not be empty";
		}
		if(callback)
		{
			this._callback = callback;
		}
		if (errorMsg != null) {
			throw errorMsg;
		}
		this._udid= Capabilities.getUniqueId();
		this._deviceType = Capabilities.getPlatformOS().toLowerCase() ;
		this._osVersion =  Capabilities.getPlatformOSVersion();
		this._deviceName=Capabilities.getDeviceName();
		this._gameCode = gameCode;
		this._gameVersion = gameVersion;
		this._gameName = gameName;
		this._gameFamily = gameFamily;
		this._url = url;
		this._gameStateDBPath = gamePath;
		this._gameFamilyId= '1';  	// Game Family id is hard coded temporarily
		this._resourcesBasePath = resourcesBasePath;
		this._dbFileNames = fileNames;
		this._userInfo();
	},
	_userInfo: function(){
//		var user = Social.US.Session.getCurrentSession().user();
//		this._userId = user.gamertag;
//		this._recordId = user.recordID;
		
		Social.Common.People.getCurrentUser(['displayName', 'id'], 	
		function(error, user)
		{
			if(error)
			{
				var errorCode = error.errorCode;
				var errorDesc = error.description;	
			}
			else
			{
				this._userId = user.displayName;
				this._recordId = user.id;
				console.log("*************************USER IS*************************** "+JSON.stringify(user));
			}
			this._callback();
		}.bind(this));
		
		
//		console.log("Zulu::: Request Sent");
		//console.log("Zulu:::: USER::   "+user);
//		this._userId = user.gamertag;
	},
	
//	userInfoCallback:function(error, user)
//	{
//		if(error)
//		{
//			console.log("Zulu::: Error is "+error.description);
//			return;
//		}
//		console.log("Zulu:::: USER::   "+user);
//	},
	
	//setRecordID:function()
	
	
	getRecordID: function()
	{
		return this._recordId;
	},
	
	getUdid : function(){
		return this._udid;
	},
	getGameStateDBPath: function(){
		return this._gameStateDBPath;
	},
	getDeviceType: function(){
		return this._deviceType;
	},
	getDeviceName: function(){
		return this._deviceName;
	},
	getOsVersion: function(){
		return this._osVersion;
	},
	getGameCode: function(){
		return this._gameCode ;
	},
	getGameVersion: function(){
		return this._gameVersion;
	},
	getGameName: function(){
		return this._gameName;
	},
	getGameFamily : function(){
		return this._gameFamily;
	},
	getURL : function(){
		return this._url;
	},
	getGameFamilyId: function(){
		return this._gameFamilyId;
	},
	getResourcesBasePath: function() {
		return this._resourcesBasePath;
	},
	getUserId: function()
	{
		return this._userId;
	},
	getDbFileNames: function(){
		return this._dbFileNames;
	},
	getUserGameStateDBPath: function(){
		var path = this._gameStateDBPath + "/" + this._userId;
		return path;
	},
	getGameXp: function(){
		return this._experience;
	},
	getGameLevel: function(){
		return this._level;
	},
	setDbFileNames: function(filenames){
		this._dbFileNames = filenames;
	},
	setGameXp: function(xp){
		this._experience = xp;
	},
	setGameLevel: function(level){
		this._level = level;
	},
	
	getNeighborVisitMode: function()
	{
		return kGANeigborVisitMode;
	},
	
	setNeighborVisitMode: function(mode)
	{
		kGANeigborVisitMode = mode;
	},
	
	getUserGameFriendsList: function()
	{
		return this._userGameFriendsList;
	},
	
	setUserGameFriendsList: function(object)
	{
		delete this._userGameFriendsList;
		this._userGameFriendsList = null;
		
		if(object.length > 0)
		{
			this._userGameFriendsList = object.sort(this.sortByGamertag);
		}
		else
		{
			this._userGameFriendsList = object;
		}
	},
	
	getUserFriendsWithGame: function(user)
	{
		if(!GAConfigurations.getNeighborVisitMode())
		{
			this.getTotalMobageFriends(user);
		}
	},
	
	getTotalMobageFriends: function(user)
	{
		var totalFriends = 0;
		var list = user.getMobageFriendsList();
		list.forAll(
			function(continueCb, friend, index){
				if(friend)
				{
					totalFriends++;
					continueCb(true);
				}
				else
				{
					continueCb(false);
				}
			},
			function(error)
			{
				GAConfigurations.getFriendsWithGame(user, totalFriends);
			}
		);
	},
	
	getFriendsWithGame: function(user, totalFriends)
	{
		var friendsWithGame = new Array();
		
		Social.Common.People.getFriendsWithGame(user.recordID, ['nickname'], {'start': 1, 'count': totalFriends}, 
			function(error, friends){
				if(friends)
				{
					for (var i = 0; i < friends.length; i++)
					{
						if(friends[i].hasApp)
						{
							var friend = new GAMobageFriend();
							
							friend.setRecordID(friends[i].id);
							friend.setThumbnailUrl(friends[i].thumbnailUrl);
							friend.setGamertag(friends[i].nickname);
							
							friendsWithGame.push(friend);
						}
					}
					GAConfigurations.excludeNonMutualFriends(friendsWithGame);
				}
			});
	},
	
	excludeNonMutualFriends: function(friendsWithGame)
	{
		var gameFriends = friendsWithGame;
		
		if(gameFriends.length > 0)
		{
			var user = Social.US.Session.getCurrentSession().user();
			
			var mobageFriendsList = null;
			mobageFriendsList =	user.getMobageFriendsList();
			mobageFriendsList.forAll(
				function(continueCb, friend, index)
				{
					if(friend)
					{
						var idx = GAConfigurations.existsInArray(gameFriends, friend);
						
						if(idx > -1)
						{
							if(!friend.isMutualFriend)
							{
								gameFriends.splice(idx, 1);
							}
						}
						continueCb(true);
					}
				},
				function(error)
				{
					GAConfigurations.setUserGameFriendsList(gameFriends);
				}
			);
		}
		else
		{
			GAConfigurations.setUserGameFriendsList(gameFriends);
		}
	},
	
	existsInArray: function(gameFriends, friend){
		for (var count = 0; count < gameFriends.length; count++){
			if(friend.recordID == gameFriends[count].getRecordID()){
				return count;
			}
		}
		return -1;
	},
	
	sortByGamertag: function (a, b)
	{
		var A = a.getGamertag().toString().toLowerCase();
		var B = b.getGamertag().toString().toLowerCase();
		
		if (A < B) {
			return -1;
		} else if (A > B) {
			return  1;
		} else {
			return 0;
		}
	},
	
	destroy : function(){
		this._udid = null;
		this._responseObject = null;
		this._deviceType = null;
		this._osVersion = null;
		this._gameVersion = null;
		this._gameName = null;
		this._gameFamily = null; 
		this._url = null;
		this._gameStateDBPath = null;
		this._userId = null;
		this._recordId = null;
		this.doneCallback = null;
		this._level = null;
		this._experience = null;
		
		if(this._userGameFriendsList.length > 0)
		{
			for(var i = 0, l = this._userGameFriendsList.length; i < l; i++)
			{
				this._userGameFriendsList[i].destroy();
			}
		}
		this._userGameFriendsList = null;
	}
});